package com.yssoft.controller.supervise;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yssoft.controller.BaseController;
import com.yssoft.entity.supplier.CirculationInfo;
import com.yssoft.entity.supplier.InstockInfo;
import com.yssoft.entity.supplier.OutstockInfo;
import com.yssoft.entity.supplier.ProcurementInfo;
import com.yssoft.entity.supplier.ProductionBatch;
import com.yssoft.entity.supplier.ProductionBatchCustom;
import com.yssoft.entity.supplier.TEnterpriseCustom;
import com.yssoft.mapper.production.TLabelsMapper;
import com.yssoft.service.supervise.FlowQueryService;
import com.yssoft.service.supervise.PurchaseStatisticsService;
import com.yssoft.service.supervise.TraceabilityQueryService;
import com.yssoft.controller.supervise.ProductionInfo;

/**
 * @类名称: TraceabilityCodeController
 * @类描述: 溯源码查询控制器
 * @作者:洪丰
 * @创建时间:2018年6月13日 下午4:23:42
 */
@Controller
@RequestMapping(value = "/supervise/traceabilityCodeQuery")
public class TraceabilityCodeController extends BaseController
{
	@Resource
	private  TLabelsMapper labelsMapper;
    @Resource
    private FlowQueryService flowQueryService;
    @Resource
	private  TraceabilityQueryService traceabilityQueryService;

	/**
	 * @方法名称: list
	 * @功能描述: 跳转到溯源码查询
	 * @作者: 洪丰
	 * @创建时间:2018年6月14日 上午8:38:37
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "/list")
	public String list(Model model,String labelCode){
		Map<String, Object>  map = null;
		//条件查询
		if(StringUtils.isNoneBlank(labelCode)) {
			map=traceabilityQueryService.queryWorksheetIdByLabelCode(labelCode);
		}
		if(map!=null&& map.get("worksheetId")!=null) {
			Integer worksheetId=Integer.valueOf(map.get("worksheetId")+"");
			ProductionBatch custom=new ProductionBatch();
			custom.setWorksheetId(worksheetId);
			//商品信息
			Map<String, Object>  product=traceabilityQueryService.queryProductByWorksheetId(custom);
			//从商品信息中获取productId，用于过滤采购信息
			if(product!=null && product.get("productId")!=null) {
				custom.setProductId(Integer.valueOf(product.get("productId").toString()));
			}
			//企业信息
			TEnterpriseCustom  enterprise=traceabilityQueryService.queryEnterpriseById(custom);
			//批次信息
			ProductionBatchCustom productionBatch=traceabilityQueryService.queryProductionBatchByBatchId(custom);
			//采购信息
			List<ProcurementInfo> procurementInfoList=traceabilityQueryService.queryProcurementInfoListByWorksheetId(custom);
			//生产信息
			List<ProductionInfo > productionInfoList=traceabilityQueryService.queryProductionInfoList(custom);
			//入库信息
			InstockInfo instockInfo=traceabilityQueryService.queryInstockInfoList(map);
			//出库信息
			OutstockInfo outstockInfo=traceabilityQueryService.queryOutstockInfoList(map);
			//流通信息（订单没有和加工单做关联，所以暂时不能查询该加工单的流通信息，只能查询产品的流通信息，出库过程中没有记录出库的加工批次号）
			Map<String, Object> circulation = flowQueryService.queryFlowMap(labelCode);
			model.addAttribute("product",product);
			model.addAttribute("enterprise",enterprise);
			model.addAttribute("productionBatch",productionBatch);
			model.addAttribute("procurementInfoList",procurementInfoList);
			model.addAttribute("productionInfoList",productionInfoList);
			model.addAttribute("circulation",circulation);
			model.addAttribute("instockInfo",instockInfo);
			model.addAttribute("outstockInfo",outstockInfo);
		}
		//根据码值获得二维码图片
		if(StringUtils.isNotBlank(labelCode)) {
			Map<String, Object>  urlMap=labelsMapper.selectLableBatchAndSourceType(labelCode);
			if(urlMap!=null && null!=urlMap.get("url") && !"".equals(urlMap.get("url"))) {
				model.addAttribute("imgUrlCode",urlMap.get("url"));
			}
		}
		return "supervise/traceabilityQuery/labelFlowQueryInfo";
	}

}
